package com.sxhuayuan.parking.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "hypk_agent")
public class Agent extends BaseEntity {

	private static final long serialVersionUID = 169644396086549519L;

	/** 代理昨天分润统计redis缓存key= s+id */
	public static final String YESTERDAY_PROFIT_STATISTICS_KEY = "yesterday_profit_statistics_";

	/** 关联的业务员ID */
	private Long salesmanId;

	/** 用户名 */
	private String username;

	/** 密码 */
	private String password;

	/** 姓名 */
	private String name;

	/** 是否启用 */
	private Boolean isEnabled = true;

	/** 是否锁定 **/
	private Boolean isLocked = false;

	/** 最后登录日期 */
	private Date loginDate;

	/** 最后登录IP */
	private String loginIp;

	private Agent parent;

	/** 微信费率 % */
	private Float weixinRate;

	/** 支付宝费率 % */
	private Float alipayRate;

	/** 代理分润比例 0-1 */
	private Float profitProportion;

	/** oem 配置，不可为空 */
	private OemConfig oemConfig;

	/** 注解：排除引用实体类自身，防止引起json无限递归 */
	@JsonIgnoreProperties(ignoreUnknown = true, value = { "parent", "child" })
	private Set<AgentRelation> parentRelations = new HashSet<>();

	@JsonIgnoreProperties(ignoreUnknown = true, value = { "parent", "child" })
	private Set<AgentRelation> childRelations = new HashSet<>();

	/** 角色 */
	private Set<Role> roles = new HashSet<>();

	/** 获取业务员ID */
	public Long getSalesmanId() {
		return salesmanId;
	}

	/** 业务员ID */
	public void setSalesmanId(Long salesmanId) {
		this.salesmanId = salesmanId;
	}

	/**
	 * 获取用户名
	 * 
	 * @return 用户名
	 */
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$")
	@Length(min = 2, max = 20)
	@Column(nullable = false, updatable = true, unique = true, length = 100)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 获取密码
	 * 
	 * @return 密码
	 */
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[^\\s&\"<>]+$")
	@Length(min = 4, max = 64)
	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 获取姓名
	 * 
	 * @return 姓名
	 */
	@Length(max = 100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取是否启用
	 * 
	 * @return 是否启用
	 */
	@NotNull
	@Column(nullable = false)
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	/**
	 * 获取是否锁定
	 *
	 * @return 是否锁定
	 */
	@NotNull
	@Column(nullable = false)
	public Boolean getIsLocked() {
		return isLocked;
	}

	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}

	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	/**
	 * 获取角色
	 * 
	 * @return 角色
	 */
	@NotEmpty
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "hypk_agent_role")
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	public Agent getParent() {
		return parent;
	}

	public void setParent(Agent parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	public Set<AgentRelation> getParentRelations() {
		return parentRelations;
	}

	public void setParentRelations(Set<AgentRelation> parentRelations) {
		this.parentRelations = parentRelations;
	}

	@OneToMany(mappedBy = "child", fetch = FetchType.LAZY)
	public Set<AgentRelation> getChildRelations() {
		return childRelations;
	}

	public void setChildRelations(Set<AgentRelation> childRelations) {
		this.childRelations = childRelations;
	}

	@Column(nullable = false, columnDefinition = "float(3,2) default '0.3'")
	public Float getWeixinRate() {
		return weixinRate;
	}

	public void setWeixinRate(Float weixinRate) {
		this.weixinRate = weixinRate;
	}

	@Column(nullable = false, columnDefinition = "float(3,2) default '0.3'")
	public Float getAlipayRate() {
		return alipayRate;
	}

	public void setAlipayRate(Float alipayRate) {
		this.alipayRate = alipayRate;
	}

	@Column(nullable = false, columnDefinition = "float(3,2) default '1'")
	public Float getProfitProportion() {
		return profitProportion;
	}

	public void setProfitProportion(Float profitProportion) {

		if (profitProportion == null || profitProportion < 0) {
			this.profitProportion = 0F;
		} else if (profitProportion > 1) {
			this.profitProportion = 1F;
		} else {
			this.profitProportion = profitProportion;
		}
	}

	@ManyToOne(fetch = FetchType.LAZY)
	public OemConfig getOemConfig() {
		return oemConfig;
	}

	public void setOemConfig(OemConfig oemConfig) {
		this.oemConfig = oemConfig;
	}

}
